package com.dayuanit.dy15.atm.controller;

import com.dayuanit.dy15.atm.dto.ResponseDTO;
import com.dayuanit.dy15.atm.handler.EmailHandler;
import com.dayuanit.dy15.atm.service.UserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class UserDetailController extends BaseController{

    @Autowired
    private UserDetailService userDetailService;

    @Autowired
    private EmailHandler emailHandler;

    //TODO 绑定邮箱  激活邮箱  忘记密码  重置密码

    @RequestMapping("/usreDetail/bindEmail")
    public ResponseDTO bindEmail(String email, HttpServletRequest request) {
        //添加邮箱到个人 绑定
        userDetailService.bindEmail(getUserId(request), email);

        //发邮件 进行激活
        emailHandler.sendActiveEmail(email);
        return ResponseDTO.sucess();
    }


    @RequestMapping("/userDetail/loadEmail")
    public ResponseDTO loadEmail(HttpServletRequest request) {
        return ResponseDTO.sucess(userDetailService.loadEmail(getUserId(request)));
    }

    @RequestMapping("/userDetail/unBindEmail")
    public ResponseDTO unBindEmail(HttpServletRequest request) {
        userDetailService.unBindEmail(getUserId(request));
        return ResponseDTO.sucess();
    }


    @RequestMapping("/userDetail/forgetPasword")
    public ResponseDTO forgetPasword(String email) {
        emailHandler.sendForgetPasswordEmail(email);
        return ResponseDTO.sucess();
    }


    @RequestMapping("/userDetail/retrievePwd")
    public ResponseDTO retrievePwd(String password, String confirmPwd, HttpSession session) {
        String email = (String)session.getAttribute("email");
        long expireTime = (Long)session.getAttribute("expireTime");
        String sign = (String)session.getAttribute("sign");

        userDetailService.retrievePwd(email, expireTime, sign, password, confirmPwd);
        return ResponseDTO.sucess();
    }

}
